from project_info import *
import tkinter as tk
from tkinter import *
from tools import *

class SysInputBox():
    def __init__(self, parent, cb, message, type='str', must_set = False, deft_value = '', title=''):
        """显示输入框
        parent      (string)    父窗口
        cb          (string)    回调函数
        message     (string)    提示文本
        type        (string)    类型:str,int,number,pwd
        muset_set   (bool)      是否必填
        deft_value  (string)    默认值
        title       (string)    窗口标题
        """
        self.project = ProjectInfo().conf()
        self.tools   = Tools()

        if title == None or title == '': title = self.project['name']
        self.cb         = cb
        self.type       = type
        self.must_set   = must_set

        # 计算窗口居中位置
        screen_width = parent.winfo_screenwidth()
        screen_height = parent.winfo_screenheight()
        w = 400
        h = 146
        x = int((screen_width - w) / 2)
        y = int((screen_height - h) / 2)
        print("SysInputBox init:",screen_width,screen_height,w,h,x,y)
        modal = Toplevel(parent)
        modal.transient(parent)  # 设置为模态
        modal.grab_set()        # 阻止用户与主窗口交互
        modal.title(title)
        modal.geometry(f"{w}x{h}+{x}+{y}")
        modal.resizable(False, False)
        
        tk.Label(modal, 
                 text=message, 
                 anchor='w', 
                 justify='left', 
                 height=3, 
                 padx=10,
                 wraplength=380
                 ).pack(side='top', fill='x')

        _show = '*' if type == 'pwd' else ''
        self.input_box_entry = Entry(modal, width=40, show=_show, text=str(deft_value))
        self.input_box_entry.pack(side='left', anchor='nw', padx=10, pady=16)

        self.input_box_entry.delete(0, tk.END)  # 删除所有内容
        if deft_value != '': self.input_box_entry.insert(0, str(deft_value))

        frameRight = Frame(modal)
        frameRight.place(x=300, y=68, width=100, height=70)

        tk.Button(frameRight, text="确认", width=10, command=self.__input_box_cb).pack(side='top', padx=5)
        tk.Button(frameRight, text="取消", width=10, command=modal.destroy).pack(side='bottom')

        self.window = modal
        #绑定事件：回车、ESC
        self.window.bind("<Return>", self.__on_enter)
        self.window.bind("<Escape>", self.__on_esc)

        self.input_box_entry.focus()

    def __input_box_cb(self):
        content = self.input_box_entry.get()
        if self.must_set and len(content) == 0:
            self.tools.msg('内容不能为空！', 'warning')
            return
        if self.type == 'int' and content.isdigit() == False:
            self.tools.msg('请输入整数！', 'warning')
            return
        if self.type == 'number' and self.tools.is_num(content) == False:
            self.tools.msg('请输入数值！', 'warning')
            return
        
        self.cb(content)
        self.window.destroy()

    def __on_enter(self, event):
        self.__input_box_cb()

    def __on_esc(self, event):
        self.window.destroy()

